package com.wanmait.baohan.controller.CustomerController;

import com.wanmait.baohan.pojo.*;
import com.wanmait.baohan.service.DictionaryService;
import com.wanmait.baohan.service.ProductService;
import com.wanmait.baohan.service.SuppilerService;
import com.wanmait.baohan.vo.DataVo;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;

import java.util.List;
import java.util.Map;


//产品管理
@Controller
@RequestMapping("customerInformation")
public class ProductController {
    @Resource
    private ProductService productService;
    @Resource
    private SuppilerService suppilerService;
    @Resource DictionaryService dictionaryService;

    @GetMapping("addProductByCustomer")
    public ModelAndView addProductByCustomer()
    {
        ModelAndView modelAndView = new ModelAndView();

        List<Map<String,Object>> productTypeAMaps = dictionaryService.getProductTypeA();
        modelAndView.addObject("productTypeAMaps",productTypeAMaps);

        List<Map<String,Object>> productTypeBMaps = dictionaryService.getProductTypeB();
        modelAndView.addObject("productTypeBMaps",productTypeBMaps);

        List<Map<String,Object>> productTypeCMaps = dictionaryService.getProductTypeC();
        modelAndView.addObject("productTypeCMaps",productTypeCMaps);

        List<Map<String,Object>> productTypeDMaps = dictionaryService.getProductTypeD();
        modelAndView.addObject("productTypeDMaps",productTypeDMaps);

        List<Map<String,Object>> productTypeEMaps = dictionaryService.getProductTypeE();
        modelAndView.addObject("productTypeEMaps",productTypeEMaps);

        //modelAndView.addObject("customerId",supplierId);
        Supplier supplier = new Supplier();
        List<Supplier> suppliers = suppilerService.selectAllot(supplier);
        modelAndView.addObject("suppliers",suppliers);
        return modelAndView;
    }
    @PostMapping("addProductByCustomer")
    public String addProductByCustomer(Product product)
    {
        System.out.println(product.getDictionaryA().getId());
        System.out.println(product.getDictionaryB().getId());
        System.out.println(product.getModel());
        System.out.println(product.getSupplier().getId());
        productService.insert(product);
        return "redirect:/customerInformation/productsList";
    }
    @RequestMapping("productsDetailed")
    public ModelAndView productsDetailed(String productId,String customerId)
    {
        ModelAndView modelAndView = new ModelAndView();
        Product product = productService.findOneBy(Integer.parseInt(productId));
        modelAndView.addObject("product",product);
        System.out.println(customerId);
        modelAndView.addObject("customerId",customerId);
        return modelAndView;
    }
    @RequestMapping("/productsList")
    public void productsList()
    {

    }
    @RequestMapping("productsData")
    @ResponseBody
    public DataVo<Product> productsData(String customerName,String productName)
    {
        Product product = new Product();
        if(StringUtils.hasText(productName))
        {
            Dictionary dictionary = new Dictionary();
            dictionary.setValue(productName);
            product.setDictionaryB(dictionary);
            //System.out.println(productName);
        }
        if(StringUtils.hasText(customerName))
        {
            Supplier supplier = new Supplier();
            supplier.setName(customerName);
            product.setSupplier(supplier);
            //System.out.println(customerName);
        }
        List<Product> products = productService.findAllBy(product);
        DataVo<Product> dataVo = new DataVo<>();
        dataVo.setCount((long) products.size());
        dataVo.setData(products);
        return dataVo;
    }
    @GetMapping("/addProduct")
    public ModelAndView addProduct(String supplierId)
    {
        ModelAndView modelAndView = new ModelAndView();
        List<Map<String,Object>> productTypeAMaps = dictionaryService.getProductTypeA();
        modelAndView.addObject("productTypeAMaps",productTypeAMaps);

        List<Map<String,Object>> productTypeBMaps = dictionaryService.getProductTypeB();
        modelAndView.addObject("productTypeBMaps",productTypeBMaps);

        List<Map<String,Object>> productTypeCMaps = dictionaryService.getProductTypeC();
        modelAndView.addObject("productTypeCMaps",productTypeCMaps);

        List<Map<String,Object>> productTypeDMaps = dictionaryService.getProductTypeD();
        modelAndView.addObject("productTypeDMaps",productTypeDMaps);

        List<Map<String,Object>> productTypeEMaps = dictionaryService.getProductTypeE();
        modelAndView.addObject("productTypeEMaps",productTypeEMaps);

        modelAndView.addObject("customerId",supplierId);
        //System.out.println(supplierId);

        return modelAndView;
    }
    @PostMapping("addProduct")
    public void addProduct(Product product)
    {
        /*System.out.println(product.getDictionaryA().getId());
        System.out.println(product.getModel());
        System.out.println(product.getRemarks());
        System.out.println(product.getRemarks());*/
        //System.out.println(product.getSupplier.getId());
        productService.insert(product);
    }
    @GetMapping("/addIndicators")//addIndicators
    public ModelAndView addIndicators(String productId, String supplierId)
    {
        ModelAndView modelAndView = new ModelAndView();
        //System.out.println("fsdfdf");
       /* System.out.println(productId);*/
        //System.out.println("32131f");
       /* System.out.println(supplierId);*/
        List<Dictionary> dictionaryListCH = suppilerService.select(25);
        List<Dictionary> dictionaryListPH = suppilerService.select(24);

       modelAndView.addObject("supplierId",supplierId);
        modelAndView.addObject("productId",productId);
        modelAndView.addObject("dictionaryListCH",dictionaryListCH);
        modelAndView.addObject("dictionaryListPH",dictionaryListPH);
        return modelAndView;
    }
    @PostMapping("/addIndicators")
    public void addIndicators(ChemicalIndex chemicalIndex, PhysicalIndex physicalIndex, String indicator1, String indicator2, String productId)
    {
       /* System.out.println(indicator1);
        System.out.println(indicator2);
         System.out.println(productId);
        System.out.println(chemicalIndex.getName());*/
    }
}
